package com.yanqu.road.server.cmd.union.war;

import com.yanqu.road.logic.union.UnionWarManger;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.unionwar.UnionWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.UnionWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_WAR_USE_BOOK, desc = "使用秘籍")
public class UseBookCmd extends AbstractCommand {

    private int bookId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarProto.UseBookReqMsg reqMsg = UnionWarProto.UseBookReqMsg.parseFrom(packet.getBytes());
        bookId = reqMsg.getBookGoodsId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        //是否备战期
        if (!UnionWarManger.isPrepareTime()) {
            ret = GameErrorCode.E_UNION_WAR_NO_PREPARE_TIME;
            return;
        }

        if (bookId != GameConfig.GAME_ITEM_BOOK_1 && bookId != GameConfig.GAME_ITEM_BOOK_2 &&
                bookId != GameConfig.GAME_ITEM_BOOK_3 && bookId != GameConfig.GAME_ITEM_BOOK_4) {
            ret = GameErrorCode.E_MALL_GOODS_TYPE_ERROR;
            return;
        }

        ret = UnionWarMgr.useBook(player, bookId);
    }

    @Override
    public void sendData(GamePlayer player) {
        UnionWarProto.UseBookRespMsg.Builder respMsg = UnionWarProto.UseBookRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_UNION_WAR_USE_BOOK, respMsg);
    }
}
